const appId = 100001
const appHost = 'https://qiaumy.isimi.net/app'

const HttpRequest = {
	post:function(url, data, callback, dataType, options) {
		if(options.loading){
			if(options.loadingTxt){
				uni.showLoading({
					title: options.loadingTxt,
					mask:true
				});
			}else{
				uni.showLoading({
					title: '加载中..',
					mask:true
				});
			}
		}
		
		var header = {};
		header.token = uni.getStorageSync('token');
		header.appid = appId;
		header.platform = uni.getStorageSync('platform_code');
		header.appversion = uni.getStorageSync('app_version_flag');
		
		header = Object.assign(header, {'content-type': 'application/x-www-form-urlencoded'});

		var param = data;

		uni.request({
			url: appHost+url,
			data: param,
			dataType:dataType,
			header: header,
			method: 'post',
			success: (res) => {
				console.log(url + ' || ' + JSON.stringify(param) + ' || ' + JSON.stringify(res.data));
				if(options.loading){
					uni.hideLoading();
				}
				
				var data = res.data;
				
				var header = res.header;
				if(data.code == 401 || data.code == 402){//需要登录而登录已失效或未登录
					uni.removeStorageSync('token');
					// uni.redirectTo({
					// 	url:'/pages/auth/auth'
					// });	
					callback(data);
					return;
				}
				if(dataType != "json"){
					callback(res.data);
					return;
				}
				// console.log(url);
				// console.log(param);
				// console.log(res.data);
				
				if(!data){
					uni.showToast({
						title: '服务器异常',
						icon:'none'
					})
					
					return;
				}
				//系统处理失败 或者 需要强制显示提示
				if(data.code == 1){
					uni.showToast({
						title: data.msg,
						icon:'none'
					})
					callback(data);
					return;
				}

				if(options.toast){
					uni.showToast({
						title:data.msg,
						duration:1500,
						icon:'none',
						showCancel:true,
						complete:function(){
							setTimeout(function(){
								callback(data)
							}, 1500)
						}
					})
				}else{
					callback(data)
				}
			}
		});
	},

	uploadFile:function(url, file, data, callback, onProgressUpdate){
		if(!onProgressUpdate){
			uni.showLoading({
				title: '上传中..',
				mask:true
			});	
		}

		var header = {};
		header.token = uni.getStorageSync('token');
		header.platform = uni.getStorageSync('platform_code');
		header.appversion = uni.getStorageSync('app_version_flag');
		header.appid = appId;
		
		if(uni.getStorageSync('token')){
			data.token = uni.getStorageSync('token');
		}
		
		console.log(url);
		console.log(file);
		console.log(data);
		
		
		const uploadTask = uni.uploadFile({
			url: appHost+url, 
			filePath: file, 
			name: 'file',
			formData: data,
			header: header,
			success: function (uploadFileRes) {
				console.log(uploadFileRes);
				// console.log(uploadFileRes.data);
				
				var data = JSON.parse(uploadFileRes.data);
				uni.hideLoading();

				if(data.result == 1){
					uni.showToast({
						title: data.msg,
						icon:'none'
					})

					return;
				}
				
				console.log(data);
				callback(data);
			}
		});
		
		if(onProgressUpdate){
			var progress = 0;
			uploadTask.onProgressUpdate(function (res) {
				if(res.progress % 5 == 0 && progress != res.progress){
					progress = res.progress;
					onProgressUpdate(res.progress);
				}
				// console.log('上传进度' + res.progress);
				// console.log('已经上传的数据长度' + res.totalBytesSent);
				// console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
		
		
				// 测试条件，取消上传任务。
				// if (res.progress > 50) {
				// 	uploadTask.abort();
				// }
			});
		}
	},
 
	downloadFileSavePhoto:function(url){
		uni.downloadFile({
			url: url,
			success: (res) => {
				if (res.statusCode === 200) {
					uni.saveImageToPhotosAlbum({
						filePath: res.tempFilePath,
							success: function () {
								uni.showToast({
									title: "保存成功",
									icon: "none"
								});
							},
							fail:function(){
								uni.showToast({
									title: "保存失败，请稍后重试",
									icon:"none"
								});
							},
					});
				}
			},
		});
	},
	
}

export default HttpRequest;